#include <iostream>
#include <fstream>
#include <typeinfo>
#include <stdio.h>
#include <string.h>

#include "file.h"

using namespace std;

#ifdef _MSC_VER
#define seekf _fseeki64
#define tellf _ftelli64
#else
#define seekf fseeko64
#define tellf ftello64
#endif

void test_file();
int main()
{
    printf("hello, world\n");
#ifdef _WIN32
    const char *filename = "D:\\1920x804.mp4";
#else
    const char *filename = "../1920x804.mp4";
#endif
    // FILE *fp = fopen(filename, "rb");
    // if(!fp)
    //     return -1;
    // cout << sizeof(long) << endl;
    // fseek(fp, 0, SEEK_END);
    // long size = ftell(fp);
    // long long llsize = tellf(fp);
    // seekf(fp, 0, SEEK_SET);
    // printf("size: %ld\n", size);
    // printf("llsize: %lld\n", llsize);
    // char buf[10240];
    // size_t read_size;
    // while(!feof(fp)) {
    //     read_size = fread(buf, 1, 10240, fp);
    //     if(read_size != 10240)
    //         printf("read_size: %lu\n", read_size);
    // }
    // fclose(fp);

    // ifstream in(filename, ios_base::in | ios_base::binary);
    // if(!in.is_open())
    //     return -2;
    // in.seekg(0, ios_base::end);
    // auto fsize = in.tellg();
    // cout << "fsize: " << fsize << endl;
    // cout << sizeof(decltype(fsize)) << endl;
    // cout << typeid(fsize).name() << endl;
    // in.seekg(0, ios_base::beg);
    // const std::streampos largeOffset = 6137836087; // 大约4GB + 100字节
    // in.seekg(largeOffset, ios_base::beg);
    // while(!in.eof()) {
    //     in.read(buf, 10240);
    //     read_size = in.gcount();
    //     if(read_size != 10240)
    //         printf("read_size: %lu\n", read_size);
    // }
    // return 0;
    test_file();
}

void test_file() {
#ifdef _WIN32
    const char *filename = "D:\\这是一个新建文本文档.txt";
    const char *newFileName = "D:\\这是一个新建文本文档_备份.txt";
    // const char *filename = "D:\\1920x804.mp4";
    // const char *newFileName = "D:\\1920x804_bak.mp4";
#else
    const char *filename = "../1920x804.mp4";
    const char *newFileName = "../1920x804_bak.mp4";
#endif
    cout << filename << endl;
    cout << newFileName << endl;
    util::File rf;
    util::File wf;
    rf.setFileName(filename);
    wf.setFileName(newFileName);

    if(!rf.open(util::File::ReadOnly)) {
        cout << filename << " " << "open fail: " << strerror(errno) << endl;
        return;
    }

    if(!wf.open(util::File::WriteOnly)) {
        cout << newFileName << " " << "open fail: " << strerror(errno) << endl;
        rf.close();
        return;
    }
    const int MAX_SIZE = 102400;
    char buf[MAX_SIZE];
    while(!rf.eof()) {
        size_t size = rf.read(buf, MAX_SIZE);
        if(size > 0) {
            size = wf.write(buf, size);
            if(size <= 0) {
                cout << newFileName << " " << "write fail: " << strerror(errno) << endl;
                break;
            }
        }
        else {
            cout << filename << " " << "read fail: " << strerror(errno) << endl;
            break;
        }
    }
    rf.close();
    wf.close();

}
